package com.ruoyi.cbs.approval.service.impl;

import java.util.*;

import com.ruoyi.cbs.approval.domain.ApprovalConfig;
import com.ruoyi.cbs.approval.mapper.ApprovalConfigMapper;
import com.ruoyi.cbs.approval.mapper.ApprovalUserMapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cbs.approval.mapper.ApprovalResultMapper;
import com.ruoyi.cbs.approval.domain.ApprovalResult;
import com.ruoyi.cbs.approval.service.IApprovalResultService;

/**
 * 审核结果Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-12
 */
@Service
public class ApprovalResultServiceImpl implements IApprovalResultService {
    @Autowired
    private ApprovalResultMapper approvalResultMapper;
    @Autowired
    private ApprovalConfigMapper approvalConfigMapper;

    /**
     * 查询审核结果
     *
     * @param id 审核结果主键
     * @return 审核结果
     */
    @Override
    public ApprovalResult selectApprovalResultById(Long id) {
        return approvalResultMapper.selectApprovalResultById(id);
    }

    /**
     * 查询审核结果列表
     *
     * @param approvalResult 审核结果
     * @return 审核结果
     */
    @Override
    public List<ApprovalResult> selectApprovalResultList(ApprovalResult approvalResult) {
        return approvalResultMapper.selectApprovalResultList(approvalResult);
    }

    /**
     * 查询用户的审核任务列表
     * @param approvalResult
     * @return
     */
    @Override
    public List<ApprovalResult> selectUserApprovalList(ApprovalResult approvalResult) {
        return approvalResultMapper.selectUserApprovalList(approvalResult);
    }

    /**
     * 新增审核结果
     *
     * @param approvalResult 审核结果
     * @return 结果
     */
    @Override
    public int insertApprovalResult(ApprovalResult approvalResult) {
        approvalResult.setId(IdFactory.newLongId());
        approvalResult.setCreateTime(DateUtils.getNowDate());
        approvalResult.setCreateBy(SecurityUtils.getUsername());
        return approvalResultMapper.insertApprovalResult(approvalResult);
    }

    /**
     * 修改审核结果
     *
     * @param approvalResult 审核结果
     * @return 结果
     */
    @Override
    public int updateApprovalResult(ApprovalResult approvalResult) {
        approvalResult.setUpdateTime(DateUtils.getNowDate());
        approvalResult.setUpdateBy(SecurityUtils.getUsername());
        return approvalResultMapper.updateApprovalResult(approvalResult);
    }

    /**
     * 更新执行状态
     * @param approvalResult 审核结果
     * @return 结果
     */
    @Override
    public int updateExecuteStatus(ApprovalResult approvalResult) {
        approvalResult.setUpdateTime(DateUtils.getNowDate());
        approvalResult.setUpdateBy(SecurityUtils.getUsername());
        return approvalResultMapper.updateExecuteStatus(approvalResult);
    }

    /**
     * 删除 对象审核流程中 未执行的审核任务
     * @param targetId 目标ID
     * @param targetType 目标类型
     * @return 结果
     */
    @Override
    public int deleteByTargetId(Long targetId, String targetType) {
        return approvalResultMapper.deleteByTargetId(targetId, targetType);
    }

    /**
     * 删除对象审核流程中，同节点下不同人的审核任务
     * @param configId 审批配置ID
     * @return 结果
     */
    @Override
    public int deleteByConfigId(Long configId, Long targetId, String targetType) {
        return approvalResultMapper.deleteByConfigId(configId, targetId, targetType);
    }

    /**
     * 批量删除审核结果
     *
     * @param ids 需要删除的审核结果主键
     * @return 结果
     */
    @Override
    public int deleteApprovalResultByIds(Long[] ids) {
        return approvalResultMapper.deleteApprovalResultByIds(ids);
    }

    /**
     * 删除审核结果信息
     *
     * @param id 审核结果主键
     * @return 结果
     */
    @Override
    public int deleteApprovalResultById(Long id) {
        return approvalResultMapper.deleteApprovalResultById(id);
    }


    public int startStopWorkProcess(LoginUser user, Long projectId, String approvalType){
        //获取当前流程配置
        List<ApprovalConfig> approvalConfigs = approvalConfigMapper.selectApprovalConfigByType(approvalType);

        return 1;
    }

//    /**
//     * 获取当前审批节点的下一个审批节点
//     *
//     * @param
//     * @return
//     */
//    public Long getNextConfigId(List<ApprovalConfig> list, ApprovalConfig config, String approvalType) {
//        int index = list.indexOf(config);
//        int target = 0;
//        for (int i = index + 1; i <= list.size(); i++) {
//            //根据parentId和审核类型判断该层级是否为最底层
//            List<ApprovalConfig> resultList = approvalConfigMapper.selectApprovalConfigByTypeAndParentId(approvalType, list.get(i).getId());
//            //如果该节点为父节点则跳过，如果该点点不为父节点，则给当前节点新增审批记录
//            if (CollectionUtils.isNotEmpty(resultList) && resultList.size() > 0) {
//                continue;
//            } else {
//                target = i;
//                break;
//            }
//        }
//        return list.get(target).getId();
//    }

//    @Override
//    public int approval(ApprovalReqVO approvalReqVO) {
//        //获取当前用户审批的流程
//        ApprovalResult approvalResult = approvalResultMapper.selectApprovalResultByUserIdAndProjectId(approvalReqVO.getUserId(), approvalReqVO.getProjectId());
//        approvalResult.setComment(approvalReqVO.getComment());
//        approvalResult.setSignUrl(approvalReqVO.getSignUrl());
//        approvalResult.setUpdateTime(DateUtils.getNowDate());
//        approvalResult.setApprovalStatus(ApprovalStatusEnum.OK.getCode());
//        approvalResult.setApprovalTime(DateUtils.getNowDate());
//        approvalResult.setExecuteStatus(ApprovalExcuteStatusEnum.COMPLETE.getCode());
//        approvalResult.setIfOperate("1");
//        return approvalResultMapper.updateApprovalResult(approvalResult);
//    }
//
//    @Override
//    public int reject(RejectedReqVO rejectedReqVO) {
//        ApprovalResult approvalResult = approvalResultMapper.selectApprovalResultByUserIdAndProjectId(rejectedReqVO.getUserId(), rejectedReqVO.getProjectId());
//        approvalResult.setComment(rejectedReqVO.getComment());
//        approvalResult.setUpdateTime(DateUtils.getNowDate());
//        approvalResult.setApprovalStatus(ApprovalStatusEnum.REJECT.getCode());
//        approvalResult.setApprovalTime(DateUtils.getNowDate());
//        approvalResult.setExecuteStatus(ApprovalExcuteStatusEnum.UNSTART.getCode());
//        return approvalResultMapper.updateApprovalResult(approvalResult);
//    }

}
